<template>
  <el-space
    fill
    wrap
    fill-ratio="50"
    direction="vertical"
    style="width: 100%; background-color: white; padding: 10px 0 10px 0"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="130px"
      v-loading="formLoading"
    >
      <el-form-item label="用户隐私协议" prop="userAgreement">
        <Editor v-model="formData.userAgreement" height="150px" />
      </el-form-item>
      <el-form-item label="一级标题" prop="title">
        <el-input v-model="formData.title" placeholder="心理评测风险（一级标题）" />
      </el-form-item>
      <el-form-item label="二级标题" prop="title2">
        <el-input v-model="formData.title2" placeholder="心理评测风险（二级标题）" />
      </el-form-item>
      <el-form-item label="心理评测风险" prop="assessmentRisk">
        <Editor v-model="formData.assessmentRisk" height="150px" />
      </el-form-item>
      <el-form-item label="积分说明" prop="pointsDescribe">
        <Editor v-model="formData.pointsDescribe" height="150px" />
      </el-form-item>
      <el-form-item label="用户默认头像" prop="userDefaultAvatar">
        <UploadImg v-model="formData.userDefaultAvatar" />
      </el-form-item>
      <el-form-item label="心理中心热线" prop="centerHotline">
        <el-input v-model="formData.centerHotline" placeholder="请输入心理中心热线" />
      </el-form-item>
      <el-form-item label="答题分数临界值" prop="answerScore">
        <el-input-number style="width: 100%" v-model="formData.answerScore" placeholder="请输入答题分数临界值（<=该值，中低风险题库随机20道；>该值，高风险题库随机40道）" />
      </el-form-item>
      <el-form-item label="首页背景图" prop="homeBackImg">
        <UploadImg v-model="formData.homeBackImg" />
      </el-form-item>
    </el-form>
    <el-space
      fill
      wrap
      fill-ratio="50"
      direction="horizontal"
      style="width: 100%"
    >
      <el-button @click="submitForm" type="primary" :disabled="formLoading">保 存</el-button>
    </el-space>
  </el-space>
</template>
<script setup lang="ts">
import { BasicConfigApi, BasicConfigVO } from '@/api/business/basicconfig'

/** 基本配置 表单 */
defineOptions({ name: 'BasicConfigForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  id: undefined,
  userAgreement: undefined,
  assessmentRisk: undefined,
  userDefaultAvatar: undefined,
  centerHotline: undefined,
  answerScore: undefined,
  title: undefined,
  title2: undefined,
  pointsDescribe: undefined,
  homeBackImg: undefined,
})
const formRules = reactive({
  userAgreement: [{ required: true, message: '用户隐私协议不能为空', trigger: 'blur' }],
  assessmentRisk: [{ required: true, message: '心理评测风险不能为空', trigger: 'blur' }],
  userDefaultAvatar: [{ required: true, message: '用户默认头像不能为空', trigger: 'blur' }],
  centerHotline: [{ required: true, message: '心理中心热线不能为空', trigger: 'blur' }],
  answerScore: [{ required: true, message: '答题分数临界值不能为空', trigger: 'blur' }],
  title: [{ required: true, message: '一级标题不能为空', trigger: 'blur' }],
  title2: [{ required: true, message: '二级标题不能为空', trigger: 'blur' }],
  pointsDescribe: [{ required: true, message: '积分说明不能为空', trigger: 'blur' }],
  homeBackImg: [{ required: true, message: '首页背景图不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async () => {
  resetForm()
  formLoading.value = true
  try {
    formData.value = await BasicConfigApi.getBasicConfig(1)
  } finally {
    formLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BasicConfigVO
    await BasicConfigApi.createBasicConfig(data)
    message.success(t('common.createSuccess'))
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userAgreement: undefined,
    assessmentRisk: undefined,
    userDefaultAvatar: undefined,
    centerHotline: undefined,
    answerScore: undefined,
    title: undefined,
    title2: undefined,
    pointsDescribe: undefined,
    homeBackImg: undefined,
  }
  formRef.value?.resetFields()
}

/** 初始化 **/
onMounted(() => {
  open()
})
</script>
